package NWish
{
	import NWish.Interface.IApp;
	
	import flash.events.KeyboardEvent;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.colorchooser.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;

	public class MenuBar
	{
		private var _app:IApp;
		
		private var _menubar	:JMenuBar;
		
		private var _file			:JMenu;
		private var _edit			:JMenu;
		private var _animator		:JMenu;
		//private var _game			:JMenu;
		private var _window			:JMenu;
		private var _setting		:JMenu;
		private var _help			:JMenu;
		
		//
		public function MenuBar(app)
		{
			//
			_app = app;
			
			//
			//AsWingManager.initAsStandard(_app);
			
			//
			_menubar = new JMenuBar();
			_menubar.setOpaque(true);
			_app.rootSprite.addChild(_menubar);
			
			//
			_file = new JMenu("&File");
			_menubar.addMenu(_file);
			
			//
			_edit = new JMenu("&Edit");
			_menubar.addMenu(_edit);
			
			//
			_window = new JMenu("&Window");
			_menubar.addMenu(_window);
			
			//
			_animator = new JMenu("&Animator");
			_menubar.addMenu(_animator);
			
			//
			//_game = new JMenu("&Game");
			//_menubar.addMenu(_game);
			
			//
			_setting = new JMenu("&Setting");
			_menubar.addMenu(_setting);
			
			//
			_help = new JMenu("&Help");
			_menubar.addMenu(_help);
			
			//
			pack();
			
			//
			//parent.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			//parent.addEventListener(KeyboardEvent.KEY_UP, 	onKeyUp);
			//_app.away3d.addEventListener(KeyboardEvent.KEY_UP, 	onKeyUp);
			_app.KBEventDispatcher.addEventListener(KeyboardEvent.KEY_UP, 	onKeyUp);
		}
		
		public function init()
		{
			//
			_help.addMenuItem("&About").addActionListener(_aboutMenuItemAct);
		}
		
		//
		private var _buttonGroups = new Object();
		private var _hotkeyButtons = new Object();
		public function addButton(button,buttonGroupName,hotkey)
		{
			if(buttonGroupName)
			{
				if(!_buttonGroups[buttonGroupName])
				{
					var bg:ButtonGroup = new ButtonGroup();
					_buttonGroups[buttonGroupName] = bg;
				}
				
				(_buttonGroups[buttonGroupName] as ButtonGroup).append(button);
				_menubar.append(button);
			}
			else
			{
				_menubar.append(button);
			}
			
			pack();
			
			//
			if(hotkey)
			{
				_hotkeyButtons[hotkey] = button;
			}
		}
		//
		/*private function onKeyDown(e:KeyboardEvent) : void
		{
		trace(e.keyCode);
		}*/
		private function onKeyUp(e:KeyboardEvent) : void
		{
			trace(e.target);
			
			//if(e.target != _app)
			//	return;
			
			if(e.ctrlKey || e.shiftKey)
				return;
			
			var b = _hotkeyButtons[e.keyCode];
			if(b)
			{
				if(b is AbstractButton)
				{
					b.doClick();
					_app.stageFocus();
				}
				else
				{
					throw new Error('todo ...');
				}
			}
		}
		
		
		//
		public function get menubar():JMenuBar
		{
			return _menubar;
		}
		public function get fileMenu():JMenu
		{
			return _file;
		}
		public function get editMenu():JMenu
		{
			return _edit;
		}
		public function get animatorMenu():JMenu
		{
			return _animator;
		}
		/*public function get gameMenu():JMenu
		{
			return _game;
		}*/
		public function get windowMenu():JMenu
		{
			return _window;
		}
		public function get settingMenu():JMenu
		{
			return _setting;
		}
		public function get helpMenu():JMenu
		{
			return _help;
		}
		
		
		public function pack()
		{
			Misc.pack(_menubar);
			
			_menubar.pack();
			_menubar.width += 48;
			_menubar.height = 16;
		}
		
		
		private function _aboutMenuItemAct(e):void
		{
			JOptionPane.showMessageDialog("About", "NWishEditor v0.0.1\n"
				+ "\n"
				+ "Power by Away3d and AsWing\n"
				+ "\n"
				+ "author: chxlchoosemylife@126.com\n"
				+ "license: copyright © 2011, chxlchoosemylife@126.com. All rights reserved.");
		}
		
	}
}


